package com.emergency.db.service.impl.info;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.info.RescueDao;
import com.emergency.db.domain.info.Rescue;
import com.emergency.db.service.info.RescueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

//突击抢险一队总表服务
@Service
public class RescueServiceImpl implements RescueService {
    @Autowired
    RescueDao rescueDao;
    @Autowired
    IdWorker idWorker;

    //保存
    public Long save(Rescue rescue) {
        if (CommonUtils.isNullOrEmpty(rescue.getId())) {
            rescue.setId(idWorker.nextId());
            rescue.setCreateTime(new Date());
        }
        //保存员工基本信息
        rescueDao.save(rescue);
        return rescue.getId();
    }

    //查询单项
    public Rescue findById(Long id) {
        return rescueDao.findById(id).get();
    }

    //删除
    public void delete(Long rescueId) {
        rescueDao.deleteById(rescueId);
    }

    //分页查询
    @Override
    public Page<Rescue> findList(Map searchMap, int page, int size) {
        Specification<Rescue> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC,"id");
        PageRequest pageRequest =  PageRequest.of(page-1, size,sort);
        return rescueDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<Rescue> createSpecification(Map searchMap) {
        return new Specification<Rescue>(){

            @Override
            public Predicate toPredicate(Root<Rescue> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 队名
                if (searchMap.get("team") != null && !"".equals(searchMap.get("team"))) {
                    predicateList.add(criteriaBuilder.equal(root.get("team").as(String.class), (String) searchMap.get("team")));
                }
                // 领导
                if (searchMap.get("leader") != null && !"".equals(searchMap.get("leader"))) {
                    predicateList.add(criteriaBuilder.like(root.get("leader").as(String.class), "%" + (String) searchMap.get("leader") + "%"));
                }
                // 任务
                if (searchMap.get("task") != null && !"".equals(searchMap.get("task"))) {
                    predicateList.add(criteriaBuilder.like(root.get("task").as(String.class), "%" + (String) searchMap.get("task") + "%"));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

}
